<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >

      <el-form-item label="申诉日期" prop="thisCycle">
        <el-date-picker
          v-model="queryParams.thisCycle"
          value-format="YYYY-MM-DD"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item label="环比日期" prop="lastCycle">
        <el-date-picker
          v-model="queryParams.lastCycle"
          value-format="YYYY-MM-DD"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px"/>
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px"/>
          重置
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
        >
          <Icon icon="ep:download" class="mr-5px"/>
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>

    <template v-if="list.length <= 0">
      <el-empty description="暂无数据" :style="{ height: tableHeight }" v-loading="loading"/>
    </template>

    <el-table v-loading="loading" :data="list" :stripe="true" v-if="list.length > 0" :show-overflow-tooltip="true" :style="{ height: tableHeight }">
      <el-table-column label="维度" align="center" prop="dimension" width="150" fixed/>
      <!-- 电信通信 -->
      <el-table-column label="中国电信" align="center" prop="chineseTelecom" width="120" sortable/>
      <el-table-column label="占来单量" align="center" prop="chineseTelecomOccupy" width="120"/>
      <el-table-column label="上周期" align="center" prop="chineseTelecomLastWeek" width="120" sortable/>
      <el-table-column label="周环比" align="center" prop="chineseTelecomWeek" width="120"/>
      <!-- 移动通信 -->
      <el-table-column label="中国移动" align="center" prop="chineseMobile" width="120"  sortable/>
      <el-table-column label="占来单量" align="center" prop="chineseMobileOccupy" width="120"/>
      <el-table-column label="上周期" align="center" prop="chineseMobileLastWeek" width="120" sortable/>
      <el-table-column label="周环比" align="center" prop="chineseMobileWeek" width="120"/>
      <!-- 联通通信 -->
      <el-table-column label="中国联通" align="center" prop="chineseUnicom" width="120" sortable/>
      <el-table-column label="占来单量" align="center" prop="chineseUnicomOccupy" width="120"/>
      <el-table-column label="上周期" align="center" prop="chineseUnicomLastWeek" width="120" sortable/>
      <el-table-column label="周环比" align="center" prop="chineseUnicomWeek" width="120"/>
      <!-- 中国广电 -->
      <el-table-column label="中国广电" align="center" prop="chineseCable" width="120" sortable/>
      <el-table-column label="占来单量" align="center" prop="chineseCableOccupy" width="120"/>
      <el-table-column label="上周期" align="center" prop="chineseCableLastWeek" width="120" sortable/>
      <el-table-column label="周环比" align="center" prop="chineseCableWeek" width="120"/>
      <!-- 合计 -->
      <el-table-column label="合计" align="center" prop="chineseTotal" width="120" sortable/>
      <el-table-column label="占总来单量" align="center" prop="chineseTotalOccupy" width="150"/>
      <el-table-column label="上周期" align="center" prop="chineseTotalLastWeek" width="120" sortable/>
      <el-table-column label="周环比" align="center" prop="chineseTotalWeek" width="120"/>
      <el-table-column label="周环比量" align="center" prop="chineseTotalWeekOccupy" width="120" sortable/>
    </el-table>

  </ContentWrap>

</template>

<script setup lang="ts">
// import { dateFormatter } from '@/utils/formatTime'
// import download from '@/utils/download'
import {HotIssueReportApi, HotIssueReportRespVO} from './index'
import download from '@/utils/download'

/** 工单清单 列表 */
defineOptions({name: 'DayWeekReport'})

const tableHeight = ref('')
const message = useMessage() // 消息弹窗
// const {t} = useI18n() // 国际化

const loading = ref(false) // 列表的加载中

const list = ref<HotIssueReportRespVO[]>([]) // 列表的数据

const queryParams = reactive({
  thisCycle: [],
  lastCycle: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getHotIssueReport = async () => {
  loading.value = true
  try {
    const data = await HotIssueReportApi.getHotIssueReport(queryParams)
    list.value = data
  } finally {
    loading.value = false
  }
}

/** 获取默认周 */
const getDefaultWeek = async () => {
  loading.value = true
  try {
    const data = await HotIssueReportApi.getDefaultWeek(queryParams)
    // console.log(data)
    queryParams.thisCycle = [data['thisStartDate'], data['thisEndDate']]
    queryParams.lastCycle = [data['lastStartDate'], data['lastEndDate']]
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  // console.log(queryParams)
  getHotIssueReport()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  getDefaultWeek()
  list.value = []
  // handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 判断list是否为空
    if (list.value.length <= 0) {
      message.warning('暂无数据，无法导出')
      return
    }
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await HotIssueReportApi.exportHotIssueReport(queryParams)
    download.excel(data, '热点问题监控报表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  tableHeight.value = (document.documentElement.clientHeight - 220)  + 'px'
  getDefaultWeek()
  // getHotIssueReport()
})
</script>

<style scoped>
/** 设置 el-table 的表头文本不自动换行 */

</style>
